//
//  MenuFenleilistViewController.m
//  Products_B
//
//  Created by lanou on 16/6/1.
//  Copyright © 2016年 朋学友. All rights reserved.
//

//  http://api.izhangchu.com/
//  cat_id=123&type=1&page=1&methodName=CategorySearch&size=20&version=4.40
#import "MenuFenleilistViewController.h"
#import "MenuFenleiListTableViewCell.h"
//#import "MenuFenleiListModel.h"
#import "MenuFirstModel.h"
#import "MenuFirstDetailViewController.h"
@interface MenuFenleilistViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)NSMutableDictionary *parDic;
@property (nonatomic, strong)NSString *page;
//  数据源
@property (nonatomic, strong)NSString *titleString;
@property (nonatomic, strong)NSString *totalString;
@property (nonatomic, strong)NSMutableArray *modelArray;

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)CGFloat width;
@property (nonatomic, strong)UILabel *caimingLabel;
@property (nonatomic ,strong)UILabel *totalLabel;;
@property (nonatomic, strong)UIButton *backButton;
@end

@implementation MenuFenleilistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modelArray = [NSMutableArray array];
    self.page = @"1";
    self.parDic = [@{@"cat_id":self.cat_id,@"type":@"1",@"page":self.page,@"methodName":@"CategorySearch",@"size":@"20",@"version":@"4.40"}mutableCopy];
    
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 60)];
    navView.backgroundColor = [UIColor whiteColor];
    self.caimingLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidth/2 - 100, 30, 100, 30)];
    self.caimingLabel.textAlignment = NSTextAlignmentRight;
    _backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _backButton.frame = CGRectMake(20, 30, 30, 30);
    //    [_backButton setTitle:@"<--" forState:(UIControlStateNormal)];
    [_backButton setBackgroundImage:[UIImage imageNamed:@"menuBack"] forState:(UIControlStateNormal)];
    [_backButton addTarget: self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [navView addSubview:_backButton];

    
    self.totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidth/2, 30, 100, 30)];
    self.totalLabel.textAlignment = NSTextAlignmentLeft;
    [navView addSubview:_caimingLabel];
    [navView addSubview:_totalLabel];
    
    [self.view addSubview:navView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, kWidth, kHeight - 100) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MenuFenleiListTableViewCell class] forCellReuseIdentifier:@"MenuFenleiListTableViewCell"];
    [self.view addSubview:_tableView];
    
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] + 1];
        self.parDic[@"page"] = self.page;
        [self requestData];
        
    }];
    [self requestData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (void)backButtonAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFirstDetailViewController *menuDetailVc = [[MenuFirstDetailViewController alloc]init];
    MenuFirstModel *model = self.modelArray[indexPath.row];
    menuDetailVc.dishes_id = model.dishes_id;
    [self.navigationController pushViewController:menuDetailVc animated:YES];
  
}
#pragma mark - 当push的时候隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFenleiListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuFenleiListTableViewCell" forIndexPath:indexPath];
 
    MenuFirstModel *model = self.modelArray[indexPath.row];
    
    cell.model = model;

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}
#pragma  mark - 请求数据 - 
- (void)requestData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
       self.titleString = jsonDic[@"data"][@"page_title"];
        self.totalString = jsonDic[@"data"][@"total"];
        self.caimingLabel.text = self.titleString;
        self.totalLabel.text = [NSString stringWithFormat:@"%@道",self.totalString];
        NSArray *array = [MenuFirstModel modelConfigureWithDic:jsonDic];
        for (MenuFirstModel *model in array) {
            [self.modelArray addObject:model];
        }
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
